<template>
	<view>
		<input placeholder="账号" v-model="postData.username" />
		<input placeholder="密码" v-model="postData.password" />
		<input placeholder="验证码" v-model="postData.code" />
		<image :src="codeImg"></image>
		<button @click="login">登录</button>
	</view>
</template>

<script>
	import db from '@/common/localstorage.js'
	export default {
		data() {
			return {
				postData: {},
				codeImg: ''
			}
		},
		onLoad() {
			console.log(9090)
			this.getCode()
		},
		methods: {
			getCode() {
				this.$get('/le/captchaImage', {}).then(res => {
					console.log(res)
					this.codeImg = 'data:image/gif;base64,' + res.data.img
					this.postData.uuid = res.data.uuid
				})
			},
			login() {
				this.$postjson('/le/login', this.postData).then(res => {
					if (res.data.code == 200) {
						this.$modal.msgInfo("登录成功")
						db.save('ACCESS_TOKEN', res.data.token)
						this.getUserInfo()
					} else {
						this.$modal.msgInfo(res.data.msg)
					}
				})
			},
			getUserInfo() {
				uni.showLoading({
					title: '加载中'
				});
				this.$get('/le/getInfo', {}).then(async res => {
					uni.hideLoading()
					if (res.data.code == 200) {
						await db.save('userInfo', res.data.user)
						await db.save('permissions', res.data.permissions)
						await db.save('roles', res.data.roles)
						uni.reLaunch({
							url: '/pages/index/index'
						})
					} else {
						this.$modal.msgInfo(res.data.msg)
					}

				})
			}


		}
	}
</script>

<style>
</style>